package ${package}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
<#list columns as col>
    <#if col.formType == '下拉框[多选]'>
import fun.yizhierha.common.utils.ArrayUtils;
        <#break>
    </#if>
</#list>
import ${package}.domain.${className};
import ${package}.domain.vo.Create${className}Vo;
import ${package}.domain.vo.Update${className}Vo;
import ${package}.domain.vo.Retrieve${className}Vo;
import ${package}.mapper.${className}Mapper;
import ${package}.service.mapstruct.${className}Mapstruct;
import ${package}.service.${className}Service;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

<#if queryHasBigDecimal >
import java.math.BigDecimal;
</#if>
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author ${author}
* @date ${date}
**/
@Service
@RequiredArgsConstructor
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service{

    private final ${className}Mapstruct ${changeClassName}Mapstruct;

    @Override
    public PageUtils<${className}> list(Retrieve${className}Vo retrieve${className}Vo, Query.PageVo pageVo) {
        QueryWrapper<${className}> wrapper = new QueryWrapper<>();
        <#list queryColumns as col>
            <#if col.formType == '下拉框[多选]'>
        String[] ${col.changeColumnName} = retrieve${className}Vo.get${col.capitalColumnName}();
                <#else>
        ${col.columnType} ${col.changeColumnName} = retrieve${className}Vo.get${col.capitalColumnName}();
            </#if>
        </#list>
        <#list betweens as bet>
        ${bet.columnType} start${bet.capitalColumnName} = retrieve${className}Vo.getStart${bet.capitalColumnName}();
        ${bet.columnType} end${bet.capitalColumnName} = retrieve${className}Vo.getEnd${bet.capitalColumnName}();
        </#list>

        <#list queryColumns as col>
        if (${col.changeColumnName} != null){
            <#if col.queryType == '='>
            wrapper.eq(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == '!='>
            wrapper.ne(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == '>='>
            wrapper.ge(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == '>'>
            wrapper.gt(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == '<='>
            wrapper.le(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == '<'>
            wrapper.lt(${className}.COL_${col.allCaps},${col.changeColumnName});
            </#if>
            <#if col.queryType == 'Like'>
                <#if col.formType == '下拉框[多选]'>
            for (String s : ${col.changeColumnName}) {
                wrapper.like(${className}.COL_${col.allCaps},s);
            }
                    <#else >
            wrapper.like(${className}.COL_${col.allCaps},${col.changeColumnName});
                </#if>
            </#if>
            <#if col.queryType == 'NotNull'>
            wrapper.isNotNull(${className}.COL_${col.allCaps});
            </#if>
        }
        </#list>

        <#list betweens as bet>
        if (start${bet.capitalColumnName} != null && end${bet.capitalColumnName} != null){
            wrapper.between(${className}.COL_${bet.allCaps},start${bet.capitalColumnName},end${bet.capitalColumnName});
        }
        </#list>

        IPage<${className}> iPage = baseMapper.selectPage(new Query<${className}>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(Create${className}Vo create${className}Vo) {
        // 1.字段为UNI，需要不重复
        <#list columns as col>
        <#if col.columnKey == 'UNI' && col.formShow>
        if (this.getOne(new QueryWrapper<${className}>()
          .eq(${className}.COL_${col.allCaps},<#if col.formType == '下拉框[多选]'>ArrayUtils.toString(create${className}Vo.get${col.capitalColumnName}())<#else>create${className}Vo.get${col.capitalColumnName}()</#if>))
          != null) {
            throw new BadRequestException(BizCodeEnum.Client_Error_CRUD.getCode(), "${col.remark}重复！");
        }
        </#if>
        </#list>
        // 2.映射数据
        ${className} ${changeClassName} = ${changeClassName}Mapstruct.to${className}(create${className}Vo);
        <#if hasTimestamp>
        <#list columns as col>
        <#if (col.dateAnnotation == "自动创建时间")! false>
        ${changeClassName}.set${col.capitalColumnName}(new Timestamp(new Date().getTime()));
        </#if>
        </#list>
        </#if>
        // 3.保存    
        this.save(${changeClassName});
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<Update${className}Vo> update${className}VoList, List<BaseErrDto> errDtoList) {
        List<${className}> toUpdate${className}List = new ArrayList<>();

        for (Update${className}Vo update${className}Vo : update${className}VoList) {
            Long id = update${className}Vo.getId();
            // 1.字段为UNI，需要不重复
            <#list columns as col>
            <#if col.columnKey == 'UNI' && col.formShow>
            if (this.getOne(new QueryWrapper<${className}>()
              .eq(${className}.COL_${col.allCaps},<#if col.formType == '下拉框[多选]'>ArrayUtils.toString(update${className}Vo.get${col.capitalColumnName}())<#else >update${className}Vo.get${col.capitalColumnName}()</#if>))
              != null) {
                BaseErrDto baseErrDto = new BaseErrDto();
                baseErrDto.setId(id);
                baseErrDto.setErrorMsg("${col.remark}重复");
                baseErrDto.setErrorField(${className}.COL_${col.allCaps});
                baseErrDto.setErrorVal(update${className}Vo.get${col.capitalColumnName}());
                errDtoList.add(baseErrDto);
                continue;
            }
            </#if>
            </#list>

            ${className} ${changeClassName} = ${changeClassName}Mapstruct.to${className}(update${className}Vo);
            <#if hasTimestamp>
            <#list columns as col>
            <#if (col.dateAnnotation == '自动更新时间')! false>
            ${changeClassName}.set${col.capitalColumnName}(new Timestamp(new Date().getTime()));
            </#if>
            </#list>
            </#if>

            toUpdate${className}List.add(${changeClassName});
        }

        // 2.更新
        this.updateBatchById(toUpdate${className}List);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"${apiAliasShort}信息表",this.list(), ${className}.class);
    }

}